package com.leyou.item.web;

import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;
    @Autowired
    private SpecParamService specParamService;


    /**
     * 根据商品分类id，查询规格组的集合
     *
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupListByCategoryId(@RequestParam("id") Long cid) {

        return ResponseEntity.ok(specGroupService.querySpecGroupListByCategoryId(cid));
    }


    /**
     * 根据规格参数组得id,查询规格参数集合
     * @param gid
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamListByGroupId(
            @RequestParam(value = "groupId",required = false) Long gid,
            @RequestParam(value = "categoryId",required = false) Long cid,
            @RequestParam(value = "searching",required = false) Boolean searching) {
        return ResponseEntity.ok(specParamService.querySpecParamListByGroupId(gid,cid,searching));
    }


    /**
     * 根据分类id查询规格组,规格参数
     * @param cid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupListById(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(specGroupService.querySpecGroupListById(cid));
    }


    /**
     * 新增规格组
     * @param groupDTO
     * @return
     */
    @PostMapping("group")
    public ResponseEntity<Void> addSpecGroup(@RequestBody SpecGroupDTO groupDTO) {
        specGroupService.addSpecGroup(groupDTO);
        return ResponseEntity.status(201).build();
    }

    /**
     * 修改分组
     * @param groupDTO
     * @return
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO groupDTO) {
        specGroupService.updateSpecGroup(groupDTO);
        return ResponseEntity.status(204).build();
    }



    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("param")
    public ResponseEntity<Void> addSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.addSpecParam(specParamDTO);
        return ResponseEntity.status(201).build();
    }


    /**
     * 修改规格参数
     * @param specParamDTO
     * @return
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        specParamService.updateSpecParam(specParamDTO);
        return ResponseEntity.status(204).build();
    }

}
